package dom.example.lucene.controller;

import bom.example.common.common.Results;
import bom.example.common.utils.PathUtils;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.UserService;
import dom.example.lucene.utils.LuceneUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author X.I.O
 * @title: LunceneController
 * @projectName demo
 * @description: TODO
 * @date 2021/11/2 19:47
 */
@Api(tags = {"luncene搜索"})
@RestController
@RequestMapping("/luncene")
public class LunceneController {

    @Autowired
    private UserService userService;

    @ApiOperation("索引列表")
    @PostMapping("indexList")
    public JSONObject indexListController(){
        List<String> strings=LuceneUtils.getIndexList();
        return Results.success(strings);
    }

    @ApiOperation("索引创建")
    @PostMapping("/create")
    public JSONObject indexCreateController(@RequestParam("indexName")String indexName) throws Exception{
        List<Map<String, Object>> mapList=userService.listMaps();
        LuceneUtils.addListDocumentByIndexName(indexName,mapList);
        return Results.success();
    }

    @ApiOperation("通过索引获取所有数据")
    @PostMapping("/getAllListByIndexName")
    public JSONObject getAllListController(@ApiParam("索引名称") @RequestParam("indexName")String indexName,
                                           @ApiParam("属性名称")@RequestParam("field")String field,
                                           @ApiParam("关键字")@RequestParam("keyword")String keyword,
                                           @ApiParam("查询的条数")@RequestParam(value = "total",defaultValue = "100")Integer total) throws Exception{
        List<Map<String, Object>> maps=LuceneUtils.searchByKeyWord(indexName,field,keyword,total);
        return Results.success(maps);
    }

    @ApiOperation("搜索")
    @PostMapping("/searchListByIndexName")
    public JSONObject searchByIndexName(@ApiParam("索引名称") @RequestParam("indexName")String indexName,
                                        @ApiParam("属性名称")@RequestParam("field")String field,
                                        @ApiParam("关键字")@RequestParam("keyword")String keyword,
                                        @ApiParam("查询的条数")@RequestParam(value = "total",defaultValue = "100")Integer total) throws Exception{
        List<Map<String, Object>> mapList=LuceneUtils.searchMapByKey(indexName,field,keyword,total);
        return Results.success(mapList);
    }

    @ApiOperation("模糊搜索")
    @PostMapping("/likesearch")
    public JSONObject likeSearchByIndexName(@ApiParam("索引名称") @RequestParam("indexName")String indexName,
                                            @ApiParam("属性名称")@RequestParam("field")String field,
                                            @ApiParam("关键字")@RequestParam("keyword")String keyword,
                                            @ApiParam("查询的条数")@RequestParam(value = "total",defaultValue = "100")Integer total) throws Exception{
        List<Map<String, Object>> mapList=LuceneUtils.likeSearch(indexName,field,keyword,total);
        return Results.success(mapList);
    }

}
